﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace ttg_internal.utils
{
    /// <summary>
    /// Contains performance information for one test. Can contain some subrecords.
    /// </summary>
    public class PerfRecord : PerfElement
    {
        private string _value;

        /// <summary>
        /// Returns test result as string-based value.
        /// Can be null (e.g. real value is contained in childs).
        /// </summary>
        public string Value
        {
            get
            {
                return _value;
            }
        }

        /// <summary>
        /// Creates leaf-type record with full information about test.
        /// </summary>
        public PerfRecord(string Name, string Description, string Value)
            :base(Name, Description)
        {
            this._value = Value;
        }

        /// <summary>
        /// Version for simple leaf-record.
        /// </summary>
        public PerfRecord(string Name, string Value)
            : this(Name, "Not provided", Value)
        {
        }

        /// <summary>
        /// Stores current record and its childs to xml.
        /// </summary>
         public override void SaveToXml(XmlDocument doc, XmlNode parent)
        {
            XmlNode root = parent.AppendChild(doc.CreateElement(this.Name));
            //Storing description.
            if (this.Description != null && this.Description != "")
            {
                XmlNode desc = doc.CreateElement("Description");
                root.AppendChild(desc);
                desc.AppendChild(doc.CreateTextNode(this.Description));
            }
            //Storing value.
            if (this.Value != null && this.Value != "")
            {
                XmlNode desc = doc.CreateElement("Value");
                root.AppendChild(desc);
                desc.AppendChild(doc.CreateTextNode(this.Value));
            }
            //Storing childs.
            foreach (PerfElement el in Childs)
                el.SaveToXml(doc, root);
        }

        public override void SaveToHtml(StringBuilder html)
        {
            //Storing element.
            html.Append(String.Format(@"<s3>{0} = {1}</s3><br/>" + Environment.NewLine,
                                      Name, Value));
            //Storing childs.
            foreach (PerfElement el in Childs)
                el.SaveToHtml(html);
        }
    }
}
